#!/bin/sh
# shellcheck shell=dash disable=SC2169,SC3010,SC3014 source=/dev/null
#
# init script to start/stop the Mediola NEOServer Addon
#

DAEMON="NeoServer"

# source all data from /var/hm_mode
[[ -r /var/hm_mode ]] && . /var/hm_mode

# skip this startup in LAN Gateway mode
[[ "${HM_MODE}" == "HM-LGW" ]] && exit 0

start() {
	# disable the watchdog if NEOServer is disabled
	if [[ -e /etc/config/neoDisabled ]] || [[ -e /usr/local/addons/mediola/Disabled ]]; then
		# make sure the watchdog is disabled
		if grep -q "/usr/local/addons/mediola/bin/watchdog" /usr/local/crontabs/root 2>/dev/null; then
			sed -i '/\/usr\/local\/addons\/mediola\/bin\/watchdog/d' /usr/local/crontabs/root
		fi
	fi

	# if NEOServer is permanently disabled remove all its userfs data
	# and exit immediately
	if [[ -e /etc/config/neoDisabled ]]; then
		rm -rf /usr/local/addons/mediola
		rm -f /etc/config/addons/www/mediola
		return
	fi

	echo -n "Starting ${DAEMON}: "

	if [[ ! -e /etc/config/safemode ]]; then

		# perform an automatic update of the neo server addon
		# if outdated.
		if [[ -d /usr/local/addons/mediola ]]; then
			version() { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }
			if [[ $(version "$(cat /opt/mediola/VERSION 2>/dev/null)") -gt $(version "$(cat /usr/local/addons/mediola/VERSION 2>/dev/null)") ]]; then
				DISABLED=0
				[[ -f /usr/local/addons/mediola/Disabled ]] && DISABLED=1

				rm -rf /usr/local/addons/mediola
				/opt/mediola/startup.sh 2>/dev/null >/dev/null

				[[ ${DISABLED} -eq 0 ]] && rm -f /usr/local/addons/mediola/Disabled
			fi
		fi

		# migration for existing installations: check for .nobackup file and create it if it does not exists
		if [[ -d /usr/local/addons/mediola ]] &&
			[[ ! -f /usr/local/addons/mediola/.nobackup ]]; then
			touch /usr/local/addons/mediola/.nobackup
		fi

		if [[ ! -e /usr/local/addons/mediola/Disabled ]]; then
			/opt/mediola/startup.sh 2>/dev/null >/dev/null
			/usr/local/addons/mediola/bin/watchdog 2>/dev/null >/dev/null
			echo "OK"
		else
			echo "disabled"
		fi
	else
		echo "skipping (safemode)"
	fi
}

stop() {
	# if NEOServer is permanently disabled exit immediately
	[[ -e /etc/config/neoDisabled ]] && return

	echo -n "Stopping ${DAEMON}: "
	if [[ -f /usr/local/addons/mediola/rc.d/97NeoServer ]]; then
		/usr/local/addons/mediola/rc.d/97NeoServer stop 2>/dev/null >/dev/null
		echo "OK"
	else
		echo "disabled"
	fi
}

restart() {
	stop
	sleep 2
	start
}

case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	restart|reload)
		restart
	;;
	*)
		echo "Usage: $0 {start|restart}"
		exit 1
esac

exit 0
